package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.DDManagerDAO;
import com.foreveross.vo.ExpressCompany;
import com.foreveross.vo.HrCode;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.ProductType;
@Component("ddManagerDAO")
public class DDManagerDAOImp extends SQLUtil implements DDManagerDAO {	
	
	public List<HrCode> findCodeList(String type,String typeName,String codeDesc,String houseId,int pageIndex,
			int pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer("select model from HrCode model where 1=1 ");
		if(!"".equals(type) && null != type){
			queryString.append(" and model.type like '%"+type+"%'");
		}
		if(!"".equals(typeName) && null != typeName){
			queryString.append(" and model.typeName like '%"+typeName+"%'");
		}
		if(!"".equals(codeDesc) && null != codeDesc){
			queryString.append(" and model.codeDesc like '%"+codeDesc+"%'");
		}
		if(!"".equals(houseId) && null != houseId){
			queryString.append(" and model.id in (select a.hrCode.id from SysCodeHouse a where a.hrHouses='"+houseId+"')");
		}
		queryString.append(" order by model.type asc,model.typeName asc,model.num asc");
		return (List<HrCode>)findObjectAll(queryString.toString(), pageIndex,pageSize);
	}
	
	
	@Override
	public long findCodeListCount(String type, String typeName, String codeDesc,String houseId)
			throws Exception {
		StringBuffer queryString = new StringBuffer("select count(*) from HrCode model where 1=1 ");
		if(!"".equals(type) && null != type){
			queryString.append(" and model.type like '%"+type+"%'");
		}
		if(!"".equals(typeName) && null != typeName){
			queryString.append(" and model.typeName like '%"+typeName+"%'");
		}
		if(!"".equals(codeDesc) && null != codeDesc){
			queryString.append(" and model.codeDesc like '%"+codeDesc+"%'");
		}
		if(!"".equals(houseId) && null != houseId){
			queryString.append(" and model.id in (select a.hrCode.id from SysCodeHouse a where a.hrHouses='"+houseId+"')");
		}
		return (Long)findObjectAll(queryString.toString()).get(0);
	}
	
	@Override
	public void deleteCode(String codeId) throws Exception{
		final String sql = "delete from HrCode t where t.codeId='"+codeId+"'";
		updateObejctByHql(sql);
	}


	@Override
	public long findProductTypeListCount(ProductType productType) throws Exception{
		StringBuffer queryString = new StringBuffer("select count(*) from ProductType model where 1=1 ");
		if(null !=productType && !"".equals(productType.getProCode()) && null != productType.getProCode()){
			queryString.append(" and model.proCode like '%"+productType.getProCode()+"%'");
		}
		if(null !=productType && !"".equals(productType.getProLineCode()) && null != productType.getProLineCode()){
			queryString.append(" and model.proLineCode like '%"+productType.getProLineCode()+"%'");
		}
		if(null !=productType && !"".equals(productType.getProLineName()) && null != productType.getProLineName()){
			queryString.append(" and model.proLineName like '%"+productType.getProLineName()+"%'");
		}
		if(null !=productType && !"".equals(productType.getProName()) && null != productType.getProName()){
			queryString.append(" and model.proName like '%"+productType.getProName()+"%'");
		}
		return (Long)findObjectAll(queryString.toString()).get(0);
	}


	@Override
	public List<ProductType> findProductTypeList(ProductType productType,
			int pageIndex, int pageSize) throws Exception{
		StringBuffer queryString = new StringBuffer("select model from ProductType model where 1=1 ");
		if(null !=productType && !"".equals(productType.getProCode()) && null != productType.getProCode()){
			queryString.append(" and model.proCode like '%"+productType.getProCode()+"%'");
		}
		if(null !=productType && !"".equals(productType.getProLineCode()) && null != productType.getProLineCode()){
			queryString.append(" and model.proLineCode like '%"+productType.getProLineCode()+"%'");
		}
		if(null !=productType && !"".equals(productType.getProLineName()) && null != productType.getProLineName()){
			queryString.append(" and model.proLineName like '%"+productType.getProLineName()+"%'");
		}
		if(null !=productType && !"".equals(productType.getProName()) && null != productType.getProName()){
			queryString.append(" and model.proName like '%"+productType.getProName()+"%'");
		}
		queryString.append(" order by model.proCode,model.proLineCode desc");
		return (List<ProductType>)findObjectAll(queryString.toString(), pageIndex,pageSize);
	}


	@Override
	public void deleteProductType(String id) throws Exception {
		final String hql = "delete from ProductType t where t.id='"+id+"'";
		updateObejctByHql(hql);
	}


	@Override
	public long findExpressCompanyCount(String name) throws Exception {
		StringBuffer queryString = new StringBuffer("select count(*) from ExpressCompany model where 1=1 ");
		if(!"".equals(name) && null != name){
			queryString.append(" and model.name like '%"+name+"%'");
		}
		return (Long)findObjectAll(queryString.toString()).get(0);
	}


	@Override
	public List<ExpressCompany> findExpressCompanyList(String name,
			int pageIndex, int pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer("select model from ExpressCompany model where 1=1 ");
		if(!"".equals(name) && null != name){
			queryString.append(" and model.name like '%"+name+"%'");
		}
		queryString.append(" order by model.id desc");
		return (List<ExpressCompany>)findObjectAll(queryString.toString(), pageIndex,pageSize);
	}
	
	
	@Override
	public List<ExpressCompany> findAllExpressCompany() throws Exception {
		StringBuffer queryString = new StringBuffer("select model from ExpressCompany model where 1=1 ");
		queryString.append(" order by model.sort asc");
		return (List<ExpressCompany>)findObjectAll(queryString.toString());
	}


	@Override
	public void deleteExpressCompanyList(String id) throws Exception {
		final String hql = "delete from ExpressCompany t where t.id='"+id+"'";
		updateObejctByHql(hql);
	}


	@Override
	public List<HrCode> getCodeList(String type,String typeName,String houseId) throws Exception {
		StringBuffer queryString = new StringBuffer("select model from HrCode model where model.typeName='"+typeName+"' and  model.type='"+type+"' and status='1' ");
		if(!"".equals(type) && null != type){
			queryString.append(" and model.type = '"+type+"'");
		}
		if(!"".equals(typeName) && null != typeName){
			queryString.append(" and model.typeName = '"+typeName+"'");
		}
		if(!"".equals(houseId) && null != houseId){
			queryString.append(" and model.id in (select a.hrCode.id from SysCodeHouse a where a.hrHouses in ('"+houseId+"'))");
		}
		queryString.append(" order by model.createTime asc");
		return (List<HrCode>)findObjectAll(queryString.toString());
	}


	@Override
	public void deleteSysCodeHouserByCode(String codeId) throws Exception {
		final String hql = "delete from SysCodeHouse t where t.hrCode='"+codeId+"'";
		updateObejctByHql(hql);
	}


	@Override
	public List<HrHouses> findHouseListByCode(String codeId) throws Exception {
		StringBuffer queryString = new StringBuffer();
		queryString.append(" select model.hrHouses from SysCodeHouse model ");
		queryString.append(" where 1=1 ");
		if(!"".equals(codeId) && null != codeId){
			queryString.append(" and model.hrCode = '"+codeId+"'");
		}
		return (List<HrHouses>)findObjectAll(queryString.toString());
	}

}
